//
//  TCommodityCategoryDAL.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/11/8.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "TCommodityCategoryDAL.h"
#import "CSFMDBTool.h"

@implementation TCommodityCategoryDAL

+ (TCommodityCategoryModel *)loadCoCategoryModelWithDict:(NSDictionary *)dict {
    //
    TCommodityCategoryModel * model = [[TCommodityCategoryModel alloc] init];
    model.caId = [dict[@"caId"] intValue];
    model.caFirstType = dict[@"caFirstType"];
    model.caSecondType = dict[@"caSecondType"];
    model.caThirdType = dict[@"caThirdType"];
    model.caFourthType = dict[@"caFourthType"];
    model.caFifthType = dict[@"caFifthType"];
    model.caWarehouseNo = dict[@"caWarehouseNo"];
    model.caWarehouse = dict[@"caWarehouse"];
    model.caCreateUserNo = dict[@"caCreateUserNo"];
    model.caCreateTime = dict[@"caCreateTime"];
    model.caRemark = dict[@"caRemark"];
    
    return model;
}

+ (NSArray<NSDictionary *> *)loadCoCategoryArrWithWarehouseNo:(NSString *)warehouseNo {
    NSString * sql = @"select * from tCommodityCategory ";
    if (warehouseNo) {
        [sql stringByAppendingFormat:@" where caWarehouseNo='%@'; ", warehouseNo];
    }else {
        [sql stringByAppendingString:@" ; "];
    }
    
    return [[CSFMDBTool SharedFmdbTool] loadDataWithSql:sql];
}

+ (NSArray<TCommodityCategoryModel *> *)loadCoCategoryModelListWithWarehouseNo:(NSString *)warehouseNo {
    NSString * sql = @"select * from tCommodityCategory ";
    if (warehouseNo) {
        [sql stringByAppendingFormat:@" where caWarehouseNo='%@'; ", warehouseNo];
    }else {
        [sql stringByAppendingString:@" ; "];
    }
    
    NSArray<NSDictionary *> *arr = [self loadCoCategoryArrWithWarehouseNo:warehouseNo];
    NSMutableArray<TCommodityCategoryModel *> * mArr = [NSMutableArray<TCommodityCategoryModel *> array];
    
    for (NSDictionary *dict in arr) {
        TCommodityCategoryModel * model = [self loadCoCategoryModelWithDict:dict];
        [mArr addObject:model];
    }
    
    return mArr.copy;
}

+ (TCommodityCategoryModel *)loadCategoryModelWithWarehouseNo:(NSString *)warehouseNo {
    NSString * sql = @"select * from tCommodityCategory ";
    if (warehouseNo) {
        [sql stringByAppendingFormat:@" where caWarehouseNo='%@'; ", warehouseNo];
    }else {
        [sql stringByAppendingString:@" ; "];
    }
    
    NSArray<NSDictionary *> *arr = [[CSFMDBTool SharedFmdbTool] loadDataWithSql:sql];
    TCommodityCategoryModel * model = [self loadCoCategoryModelWithDict:arr[0]];
    
    return model;
}

+ (NSArray<TCommodityCategoryModel *> *)loadCoCategoryModelFirstWithWarehouseNo:(NSString *)warehouseNo {
    NSString * sql = [NSString stringWithFormat:@"select distinct caFirstType from tCommodityCategory where caWarehouseNo='%@'; ", warehouseNo];
    NSArray<NSDictionary *> *arr = [[CSFMDBTool SharedFmdbTool] loadDataWithSql:sql];
    NSMutableArray<TCommodityCategoryModel *> * mArr = [NSMutableArray<TCommodityCategoryModel *> array];
    
    for (NSDictionary *dict in arr) {
        TCommodityCategoryModel * model = [self loadCoCategoryModelWithDict:dict];
        [mArr addObject:model];
    }
    return mArr.copy;
}

+ (NSArray<TCommodityCategoryModel *> *)loadCoCategoryModelSecondWithFirst:(NSString *)first warehouseNo:(NSString *)warehouseNo {
    NSString * sql = [NSString stringWithFormat:@"select distinct caSecondType from tCommodityCategory where caWarehouseNo='%@' ", warehouseNo];
    if (first) {
        sql = [sql stringByAppendingFormat:@"and caFirstType='%@'; ", first];
    }else {
        sql = [sql stringByAppendingString:@" ; "];
    }
    NSArray<NSDictionary *> *arr = [[CSFMDBTool SharedFmdbTool] loadDataWithSql:sql];
    NSMutableArray<TCommodityCategoryModel *> * mArr = [NSMutableArray<TCommodityCategoryModel *> array];
    
    for (NSDictionary *dict in arr) {
        TCommodityCategoryModel * model = [self loadCoCategoryModelWithDict:dict];
        [mArr addObject:model];
    }
    return mArr.copy;
}

+ (NSArray<TCommodityCategoryModel *> *)loadCoCategoryModelThirdWithSecond:(NSString *)second warehouseNo:(NSString *)warehouseNo {
    NSString * sql = [NSString stringWithFormat:@"select distinct caThirdType from tCommodityCategory where caWarehouseNo='%@' ", warehouseNo];
    if (second) {
        sql = [sql stringByAppendingFormat:@"and caSecondType='%@'; ", second];
    }else {
        sql = [sql stringByAppendingString:@" ; "];
    }
    NSArray<NSDictionary *> *arr = [[CSFMDBTool SharedFmdbTool] loadDataWithSql:sql];
    NSMutableArray<TCommodityCategoryModel *> * mArr = [NSMutableArray<TCommodityCategoryModel *> array];
    
    for (NSDictionary *dict in arr) {
        TCommodityCategoryModel * model = [self loadCoCategoryModelWithDict:dict];
        [mArr addObject:model];
    }
    return mArr.copy;
}






















@end
